﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraReports.UI;
using ProgNetComponents.Data.SqlCe;
using System.Data;
using System.ComponentModel;
using ProgNetComponents;

namespace MiniKsiegowosc.RaportTools
{
    public partial class MonthReportBase  : XtraReport, IMonthReport
    {
        protected SqlCeDataManager manager = new SqlCeDataManager();
        protected Dictionary<int, decimal> dicPage = new Dictionary<int, decimal>();
        protected Dictionary<int, decimal> dicTotal = new Dictionary<int, decimal>();

        private bool rowsPrepared = false;
        private int rok;
        private int miesiac;

        [Browsable(false)]
        public int Rok
        {
            get { return rok; }
            set
            {
                rok = value;
                pRok.Value = rok;
            }
        }
        
        [Browsable(false)]
        public int Miesiac
        {
            get { return miesiac; }
            set
            {
                miesiac = value;
                pMiesiac.Value = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(miesiac);
            }
        }

        [Category(Categories.ProgNetSettings),
        DisplayName("RowSumaStrony")]
        public XRTableRow RowSumaStrony
        {
            get;
            set;
        }
        
        [Category(Categories.ProgNetSettings),
        DisplayName("RowSumaPoprzedniej")]
        public XRTableRow RowSumaPoprzedniej
        {
            get;
            set;
        }
       
        [Category(Categories.ProgNetSettings)]
        public DataTable FakturyTable
        {
            get;
            set;
        }

        public MonthReportBase()
        {
            InitializeComponent();
        }

        protected void PrepareRows()
        {
            if (!rowsPrepared)
            {
                XRBinding b = null;
                for (int i = 0; i < RowSumaStrony.Cells.Count; i++)
                {
                    if (RowSumaStrony.Cells[i].Summary != null &&
                        RowSumaStrony.Cells[i].Summary.Running != SummaryRunning.None &&
                        RowSumaStrony.Cells[i].DataBindings["Text"] != null)
                    {
                        b = RowSumaStrony.Cells[i].DataBindings["Text"];

                        RowSumaPoprzedniej.Cells[i].DataBindings.Add(new XRBinding("Text", b.DataSource, b.DataMember));
                        RowSumaPoprzedniej.Cells[i].Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Custom, "{0:n2}");
                        RowSumaPoprzedniej.Cells[i].SummaryGetResult += new SummaryGetResultHandler(raportVatS_SummaryGetResult);
                    }
                }
            }
        }

        protected virtual string GetSelectString()
        {
            return string.Empty;
        }

        public virtual void GetData()
        {
            PrepareRows();
            FakturyTable.Clear();
            manager.FillTable(FakturyTable, GetSelectString());
        }

        protected void raportVatS_SummaryGetResult(object sender, SummaryGetResultEventArgs e)
        {
            XRTableCell cell = sender as XRTableCell;
            int idx = cell.Row.Cells.IndexOf(cell);
            decimal value = 0;
            foreach (object o in e.CalculatedValues)
            {
                value += o.AsDecimal();
            }
            if (dicPage.ContainsKey(idx))
                dicPage[idx] = value;
            else
                dicPage.Add(idx, value);

            if (dicTotal.ContainsKey(idx))
                dicTotal[idx] += value;
            else
                dicTotal.Add(idx, value);

            e.Result = dicTotal[idx] - dicPage[idx];
            e.Handled = true;
        }

        public virtual MonthReportBase GetClonedReport()
        {
            MonthReportBase r = null;
            try
            {
                r = Activator.CreateInstance(this.GetType()) as MonthReportBase;
            }
            catch (Exception ex)
            {
                ex.Log();
            }
            return r;
        }
    }
}
